package com.th.coupon.common.advice;


import com.th.coupon.common.exception.BaseCloudException;
import com.th.coupon.common.vo.CommonResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 * @author 涂航
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {


    /**
     * 异常响应处理方法
     * @param req
     * @param ex
     * @return
     */
    @ExceptionHandler(value = BaseCloudException.class)
    public CommonResponse<String> handlerCouponException(
            HttpServletRequest req, BaseCloudException ex
    ) {
        CommonResponse<String> response = null;
        if(null != ex.getResultData()){
            response = new CommonResponse<>(ex.getResultData());
        }else {
            response =new CommonResponse<>(000,ex.getMessage());
        }
        response.setData(ex.getMessage());
        return response;
    }

}
